﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Newtonsoft.Json;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageCommon
{
    /// <summary>
    /// 曲线图
    /// </summary>
    public class PageCommon_LineChart:ControlBase
    {
        private string _title = "";
        private YAxesConfig _yAxesConfig = null;
        private XAxesConfig _xAxesConfig = null;
        private string _xAxesTitle = "";
        private string[] _xaxesLabels = null;
        private List<LineItem> _line=new List<LineItem>();

        public PageCommon_LineChart(ViewContext viewContext) : base(viewContext)
        {
            this.Css("chartjsbox");
        }

        public PageCommon_LineChart Title(string title)
        {
            _title = title;

            return this;
        }

        public PageCommon_LineChart YAxes(Action<YAxesConfig> config)
        {
            var ctx = new YAxesConfig();

            config(ctx);

            _yAxesConfig = ctx;

            return this;
        }

        public PageCommon_LineChart XAxesTitle(string label)
        {
            _xAxesTitle = label;

            return this;
        }

        public PageCommon_LineChart XAxesLabels(string[] xaxesLabels)
        {
            _xaxesLabels = xaxesLabels;

            return this;
        }

        public PageCommon_LineChart AddLine(string label,string color,string[] data)
        {
            return this;
        }

        public override HtmlString Render()
        {
            this.Data("yaxesconfig", HttpUtility.HtmlEncode(JsonConvert.SerializeObject(_yAxesConfig, Formatting.None)))
                .Data("xaxesconfig", HttpUtility.HtmlEncode(JsonConvert.SerializeObject(_xAxesConfig, Formatting.None)))
                ;

            var names=new List<string>(_line.Count);

            for (int i = 0; i < _line.Count; i++)
            {
                var name = "line_" + i;
                this.Data(name, HttpUtility.HtmlEncode(JsonConvert.SerializeObject(_line[i], Formatting.None)));
                names.Add(name);
            }

            this.Data("linenames", HttpUtility.HtmlEncode(JsonConvert.SerializeObject(names, Formatting.None)));

            if (_withOuterBorder)
            {
                ViewContext.Writer.WriteLine($"<div class=\"{_outerBorderCss}\">");
            }

            ViewContext.Writer.WriteLine($"<div {Attributes}>");

            ViewContext.Writer.WriteLine("<canvas></canvas>");

            ViewContext.Writer.WriteLine("</div>");

            if (_withOuterBorder)
            {
                ViewContext.Writer.WriteLine($"</div> <!-- outer border -->");
            }

            return HtmlString.Empty;
        }

        private class LineItem
        {
            public LineItem()
            {

            }

            public string Label { set; get; }

            public string Color { set; get; }

            public string[] Data { set; get; }
        }

        public class YAxesConfig
        {
            public int InitMinValue { set; get; }

            public int InitMaxValue { set; get; }

            public string Label { set; get; }
            
        }

        public class XAxesConfig
        {
            public string Title { set; get; }

            public string[] Labels { set; get; }
        }
    }
}
